import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  ValidationPipe,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { AddAccessDto } from './dto/add-access.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post('add')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  create(@Body(new ValidationPipe()) createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  @Get('list')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  findAll(
    @Query('rolename') rolename: string,
    @Query('pageSize') pageSize: number,
    @Query('currentPage') currentPage: number,
  ) {
    return this.roleService.findAll(rolename ? rolename : '', {
      pageSize: pageSize ? pageSize : 100,
      currentPage: currentPage ? currentPage - 1 : 0,
    });
  }

  @Get(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  findOne(@Param('id') id: string) {
    return this.roleService.findOne(+id);
  }

  @Patch(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return this.roleService.update(id, updateRoleDto);
  }

  @Delete(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  remove(@Param('id') id: string) {
    return this.roleService.remove(id);
  }

  @Post('/add/access')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  addAccess(@Body() addAccessDto: AddAccessDto) {
    //开启事务第三步：获取事务管理器)
    return this.roleService.addAccess(addAccessDto);
  }

  @Get('/hasaccess/:id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  async findOwnerAccess(@Param('id') id: string) {
    return await this.roleService.findOwnerAccess(id);
  }
}
